/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-21     AlgoIdeas       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#include "gps.h"
#include "key.h"
#include "page.h"
#include "track.h"

static uint8_t first_located = 0;
static uint8_t green_pin = 0;
static uint8_t red_pin = 0;
static page_state_t state = 0;
static rt_int32_t page_index = PAGE_STARTUP;


/* GPS Info Callback */
static rt_int32_t gps_callback(GPS_Info_t info)
{
    static int count = 0;

    if (info.isVaild) {
        if (0 == first_located) {
            first_located = 1;
            page_index = PAGE_DATE_TIME;
            page_switch(page_index);
        }

        /* Reset Count */
        if (0 != count) {
            count = 0;
        }

        state |= PAGE_STATE_LOCATION;
        track_point(info);
        rt_pin_write(green_pin, PIN_LOW);
    }
    else {
        /* Invaild state count */
        if (count >= 2000) {
            count = 0;
            state &= (~PAGE_STATE_LOCATION);
            rt_pin_write(green_pin, PIN_HIGH);
        }

        count += 1;
    }

    return RT_EOK;
}

/* Key Event Callback */
static rt_int32_t key_callback(key_code_t code, key_event_t event)
{
    rt_kprintf("Key code [%d] event [%d]\n", code, event);

    if (1 != first_located) {
        return RT_EOK;
    }

    /*
     *  When located S2 - Page Down, S3 - Page Up
     **/
    if (KEY_EVT_DOWN == event) {
      if (KEY_CODE_S2 == code) {
          page_index -= 1;
          page_index = (page_index + PAGE_MAX) % (PAGE_MAX);
      } else if (KEY_CODE_S3 == code) {
          page_index += 1;
          page_index = (page_index + PAGE_MAX) % (PAGE_MAX);
      }

      rt_kprintf("page index [%d]\n", page_index);
      page_switch(page_index);
    } else if (KEY_EVT_HOLD == event) {
        /*
         *  S2 - Track Start, S3 - Track Stop
         **/
        if (KEY_CODE_S2 == code) {
            state |= PAGE_STATE_TRACK;
            rt_pin_write(green_pin, PIN_LOW);
            track_start();
            rt_thread_mdelay(200);
            rt_pin_write(green_pin, PIN_HIGH);
        } else if (KEY_CODE_S3 == code) {
            state &= (~PAGE_STATE_TRACK);
            rt_pin_write(red_pin, PIN_LOW);
            track_stop();
            rt_thread_mdelay(200);
            rt_pin_write(red_pin, PIN_HIGH);
        }
    }

    return RT_EOK;
}

static void main_entry(void *parameter)
{
    while (1)
    {
        page_update(state);
        rt_thread_mdelay(100);
    }

    return ;
}

int main(void)
{
    rt_thread_t tid;

    green_pin = rt_pin_get("PE.4");
    rt_pin_mode(green_pin, PIN_MODE_OUTPUT);
    rt_pin_write(green_pin, PIN_HIGH);

    red_pin = rt_pin_get("PE.1");
    rt_pin_mode(red_pin, PIN_MODE_OUTPUT);
    rt_pin_write(red_pin, PIN_HIGH);

    gps_init(gps_callback);
    key_init(key_callback);
    page_init();

    tid = rt_thread_create("main", main_entry,
            RT_NULL,
            4096,
            RT_THREAD_PRIORITY_MAX / 2,
            20);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        rt_kprintf("rt_thread_create main thread failed!");
        return RT_ERROR;
    }

    return RT_EOK;
}
